package com.cacho.s2b.lesson.utils;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * @Description 文件处理包 commons-io
 * @Author Lingwei
 * @date 2023/12/14
 **/
@Getter
@Setter
public class DearCommIo {
    public DearCommIo(String file) {
        this.file = file;
    }

    private String file; // 文件路径(/)
    // 根据文件相对路径获取 文件
    public InputStream inFile(String file) {
        InputStream in = null;
        try {
            in = this.getClass().getResourceAsStream(file);
            return in;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
    public String getIOUtilsString(InputStream input) throws IOException {
        if (input==null){
            return null;
        }
        else{
            return IOUtils.toString(input, StandardCharsets.UTF_8);
        }
    }

    public static void main(String[] args) throws IOException {
        DearCommIo io = new DearCommIo("/data/sample/test.json");
        InputStream input = io.inFile(io.getFile());
        String json =  io.getIOUtilsString(input);
        System.out.println(json);
    }

}
